package test.com;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.xinyan.pojo.BaseEntity;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.*;


@Slf4j
public class GeneratorCode {
    @Test
    public void test00() {
        String projectDir = System.getProperty("user.dir");
        FastAutoGenerator.create("jdbc:mysql://110.41.83.162:4453/spray?useSSL=false", "root", "2[i,ht.a-GY5")

                .globalConfig(builder -> {
                    builder.author("xzlf") // 设置作者
//                            .enableSwagger() // 开启 swagger 模式

                            .fileOverride() // 覆盖已生成文件
                            .disableOpenDir() //禁止打开输出目录
                            .commentDate("yyyy-MM-dd HH:mm:ss")
                            .outputDir(projectDir + "/src/main/java"); // 指定输出目录
                })

                .packageConfig(builder -> {
                    builder.parent("com.xinyan") // 设置父包名
                            .entity("pojo") //设置entity包名
                            .mapper("mapper")

//                            .other("model.dto") // 设置dto包名
//                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, projectDir + "/src/main/resources/mapper")); // 设置mapperXml生成路径

                })
                .strategyConfig(builder  ->{
                    builder.addInclude("spray_admin")
                            .entityBuilder()
                            .fileOverride()
                            .enableLombok()
                            .superClass(BaseEntity.class)
                            .enableTableFieldAnnotation()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .mapperBuilder()
                            .enableMapperAnnotation()
                            .enableBaseColumnList()
                            .enableBaseResultMap()
                            .fileOverride();
                })
//                .injectionConfig(consumer -> {
//                    Map<String, String> customFile = new HashMap<>();
//                    // DTO
//                    customFile.put("DTO.java", "/templates/entityDTO.java.ftl");
//                    consumer.customFile(customFile);
//                });
                .templateEngine(new VelocityTemplateEngine())
                .execute();
    }


    @Test
    public void test01(){
        log.info("当前路径：{}",System.getProperty("user.dir"));
    }

    @Test
    public void test02(){

        // 2[i,ht.a-GY5 --mysql
        // fc_C[v28fD{yya} --linux
        // p,3GW25424E=
        // %4A087-64=4%RSveW
        // 8,g36e,}1]8l++Y
        // qQ^wCz8Gf6pa-yV
        // X0[7o87+BhRB
        // }!pBF1FBAq7JR=cjB
        // HoM-Kn5o75J5m --redis
        // S%sbtg3=^75Ko2o
        for (int i = 0; i < 10; i++) {
            System.out.println(randomPwd());
        }
    }

    public String randomPwd(){
        List<String> list = new ArrayList<>();
        String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String LOWER = "abcdefghijklmnopqrstuvwxyz";
        String NUMBERS = "0123456789";
        String SPECIAL_CHARS = "!@%^-_=+[{}]:,./?";
        String allCharacters = UPPER + LOWER + NUMBERS + SPECIAL_CHARS;
        list.add(UPPER);
        list.add(LOWER);
        list.add(NUMBERS);
        list.add(SPECIAL_CHARS);
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        int len = 12 + random.nextInt(6);
        for (int i = 0; i < len; i++) {
            String tmp = list.get(random.nextInt(list.size()));
            sb.append(tmp.charAt(random.nextInt(tmp.length())));
        }
        return sb.toString();
    }
}
